﻿using DataMonitor_IBLL;
using DataMonitorWebApi.Quartz;
using DataMonitorMysql.MappingClass;
using DataMonitorWebApi.Utility;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Caching.Memory;
using System.IO;

namespace DataMonitorWebApi.Controllers
{
    [Route("[controller]/[action]")]
    [ApiController]
    public class QuartzController : ControllerBase
    {
        private readonly ILogger<QuartzController> _logger;
        public QuartzService QuartzService { get; }
        private readonly IMemoryCache _memoryCache;//内存缓存
        public QuartzController( ILogger<QuartzController> logger , QuartzService quartzService, IMemoryCache memoryCache)
        {
            _logger = logger;
            QuartzService = quartzService;
            _memoryCache = memoryCache;
        }


        [HttpGet]
        //  */5 * * * * ? 每隔5秒
        public async Task AddandStartJob(string jobname)
        {
            //获得监控数据路径和监控日志路径
            var pathlist=jobname.Split(';');
            if(pathlist.Length == 2 )
            {
                //监控数据的时间间隔
                var second = _memoryCache.Get<dm_props_admin>("datamonitoradminpara")?.intervaltime;
                //监控日志的时间间隔
                var logsecond = _memoryCache.Get<dm_props_admin>("datamonitoradminpara")?.logintervaltime;
                string cron1 = $"*/{second} * * * * ?";
                string cron2 = $"*/{logsecond} * * * * ?";
                string watchpath = Path.Combine(pathlist[0], DateTime.Now.ToString("MM_dd"));
                //此路径为input路径，也就是填写的路径，不是日期路径，这里其实是为了验证这个jobname路径是否存在，没有其他作用
                string? parentPath = Path.GetDirectoryName(watchpath);
                if (parentPath == null || parentPath == string.Empty)
                {
                    _logger.LogWarning($"开启定时监控路径：{watchpath} 没有父目录或没有此目录");
                    await Task.CompletedTask;
                }
                else
                {
                    await QuartzService.RemoveJob(pathlist[0]);
                    await QuartzService.RemoveJob(pathlist[1]);
                    await QuartzService.AddJob(jobname, cron1, cron2);
                    await QuartzService.Start();
                }
            }
            
           
        }

        [HttpDelete]
        public async  Task<IActionResult> RemoveJob(string key)
        {
            await QuartzService.RemoveJob(key);
           
            return Ok("取消监控成功");
        }
        [HttpGet]
        public async Task<IActionResult> DeleteJob(string jobname)
        {
            await QuartzService.RemoveJob(jobname);
            return Ok("取消监控成功");
        }
        [HttpGet]
        public async Task PauseJob(string jobName)
        {
            await QuartzService.PauseJob(jobName);
        }

        [HttpGet]
        public async Task ResumeJob(string jobName)
        {
            await QuartzService.ResumeJob(jobName);
        }

      


    }
}
